import queue
import traceback
from concurrent.futures import ThreadPoolExecutor

import cv2


class BoundThreadPoolExecutor(ThreadPoolExecutor):
    #对ThreadPoolExecutor 进行重写，给队列设置边界
    def __init__(self, qsize: int = None, *args, **kwargs):
        super(BoundThreadPoolExecutor, self).__init__(*args, **kwargs)
        self._work_queue = queue.Queue(qsize)

def test():
    while 1:
        a = 10
        try:
            s = 1/0
        except:
            print(traceback.format_exc())
            continue

    print('done')



def debug2():
    def img2mask(img0, step_mode):

        # # # 定义边框的宽度
        # top_border = 30
        # h, w = img0.shape[:2]
        # img0[sy1:top_border, ...] = 255 # 上
        # img0[sy2 - top_border:, ...] = 255 # 下
        # img0[:, sx1:top_border, ...] = 255 # 左
        # img0[:, sx2 - top_border:, ...] = 255 # 右


        # 缓存图片
        img_scale = resized_img_size / img0.shape[0]  # 方形
        # if clips_box_dict.get(step_mode):
        #     sx1, sy1, sx2, sy2 = clips_box_dict[step_mode] # clip
        #     img0 = img0[sy1:sy2, sx1:sx2]
        img_320 = cv2.resize(img0, dsize=(resized_img_size, resized_img_size))
        # img_320 = cv2.resize(img0, fx=img_scale, fy=img_scale)
        img_320 = eval(img_change_mode)
        # img_320 = img_320[::-1, :, :].transpose(1, 0, 2)
        ox, oy = int(piex_xys[step_mode - 1][0]), int(piex_xys[step_mode - 1][1])
        # mask1[oy -half_resized_img_size :oy + half_resized_img_size, ox - half_resized_img_size:ox + half_resized_img_size, ...] = img_320
        print(f'ox oy {ox} {oy}')
        # roi_clips = {
        #     'dbxh': {
        #         12: [50, 0, 640, 640]  # roi裁剪
        #     },
        #     'xbxh': {}
        # }

        if clips_box_dict.get(step_mode):
            sx1, sy1, sx2, sy2 = clips_box_dict[step_mode]
            # cv2.rectangle(img0, (sx1, sy1), (sx2, sy2), (0, 0, 255), 5)
            sx1, sy1, sx2, sy2 = int(sx1*img_scale), int(sy1*img_scale), int(sx2*img_scale), int(sy2*img_scale)

        else:
            sx1, sy1, sx2, sy2 = 0, 0, resized_img_size, resized_img_size

        # # # 定义边框的宽度
        # top_border = 5
        # img_320[sy1:sy1+top_border, ...] = 255 # 上
        # img_320[sy2 - top_border:sy2, ...] = 255 # 下
        # img_320[:, sx1:sx1+top_border, ...] = 255 # 左
        # img_320[:, sx2 - top_border:sx2, ...] = 255 # 右

        lx = resized_img_size / 2 - sx1
        rx = sx2 - resized_img_size / 2
        uy = resized_img_size / 2 - sy1
        by = sy2 - resized_img_size / 2
        dy1, dy2, dx1, dx2 = int(oy - uy), \
            int(oy + by), \
            int(ox - lx), \
            int(ox + rx)  # mask


        # 自动舍弃多余部分
        if dy2 < 0 or dx2 < 0 or dy1 > final_img_size[0] or dx1 > final_img_size[1]:
            print('坐标越界')
            return

        if dy2 > final_img_size[0]:
            sy2 -= (dy2 - final_img_size[0])
            dy2 = final_img_size[0]
        if dy1 < 0:
            sy1 -= dy1
            dy1 = 0
        if dx2 > final_img_size[1]:
            sx2 -= (dx2 - final_img_size[1])
            dx2 = final_img_size[1]
        if dx1 < 0:
            sx1 -= dx1
            dx1 = 0

        # 两种策略
        if product_str == 'dbxh':
            tm = mask1[dy1:dy2, dx1:dx2, ...]
            om = img_320[sy1:sy2, sx1:sx2, ...]
            if step_mode in [2, 5, 8, 10, 11, 12]:
                tm[...] = om[...]  # 覆盖
            else:
                zero_mask = np.all(tm == 0, axis=-1)
                tm[zero_mask, ...] = om[zero_mask, ...]  # 不覆盖
        else:
            mask1[dy1:dy2, dx1:dx2, ...] = img_320[sy1:sy2, sx1:sx2, ...]
        # mask1[dy1:dy2, dx1:dx2, ...] = mask1[dy1:dy2, dx1:dx2, ...] + img_320[sy1:sy2, sx1:sx2, ...]
        # mask1[final_img_size[0]-dy2:final_img_size[0]-dy1,dx1:dx2,...] = img_320[sy1:sy2,sx1:sx2,...][::-1,...] # +上下反转
        print(f'mask1 xy {dx2 - dx1} {dy2 - dy1}  img320 xy {sx2 - sx1} {sy2 - sy1}')
        # if step_mode==9:
        #     mask1


if __name__ == '__main__':
    pool = BoundThreadPoolExecutor(qsize=18, max_workers=18)
    pool.submit(test)